package com.leha.mp.core.handler;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.leha.mp.core.entity.mybatis.MpDoctorCode;
import com.leha.mp.core.entity.vo.DoctorBaseInfoVo;
import com.leha.utils.DateTool;
import lombok.extern.log4j.Log4j2;
import me.chanjar.weixin.mp.bean.template.WxMpTemplateData;
import me.chanjar.weixin.mp.bean.template.WxMpTemplateMessage;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Log4j2
public class TemplateMsgHelper {
    public static WxMpTemplateMessage ScanTemplateMsgBuilder(DoctorBaseInfoVo code, String openid) {
        List<WxMpTemplateData> tmplateDatas = new ArrayList<>();
        WxMpTemplateData first = new WxMpTemplateData("first", "您已成功关注"+code.getName()+code.getTitle()+"，请立即完成患者报到，方便医生认出您！\n");
        first.setColor("#FF3A3A");
        WxMpTemplateData keywords1 = new WxMpTemplateData("keyword1", code.getName()+code.getTitle());
        WxMpTemplateData keywords2 = new WxMpTemplateData("keyword2", "完善病例信息");
        WxMpTemplateData remark = new WxMpTemplateData("remark", "\n完善的个人信息有助于医生充分了解您，准确进行诊疗。");
        tmplateDatas.add(first);
        tmplateDatas.add(keywords1);
        tmplateDatas.add(keywords2);
        tmplateDatas.add(remark);

        return
                WxMpTemplateMessage.builder().templateId("r2yq8UemZB-4KyohWtnXQmazmTHm9Zl4UKsC9-2GnM0")
                        .toUser(openid)
                        .url("https://m.pule.com/kepu/")
                        .miniProgram(new WxMpTemplateMessage.MiniProgram("wx14f319c8964de262",
                                "index?foo=bar"))
                        .data(tmplateDatas)
                        .build();
    }

    public static WxMpTemplateMessage MsgPushBuilder(String data, String openid) {
        try {
            log.info("数据字段："+data);
            JSONObject obj = JSON.parseObject(data);
            List<WxMpTemplateData> tmplateDatas = new ArrayList<>();

            for(String key: obj.keySet()) {
                if(key != "templateId" && key != "appid" && key != "apppath") {
                    WxMpTemplateData d = new WxMpTemplateData(key, obj.getString(key));
                    if(key.equals("first")) {
                        d.setColor("#FF3A3A");
                    }
                    tmplateDatas.add(d);
                }
            }
            return
                    WxMpTemplateMessage.builder().templateId(obj.getString("templateId"))
                            .toUser(openid)
                            .url("https://m.pule.com/kepu/")
                            .miniProgram(new WxMpTemplateMessage.MiniProgram(obj.getString("appid"),
                                    obj.getString("apppath")))
                            .data(tmplateDatas)
                            .build();
        } catch (Exception e) {
            log.error("模板消息解析失败："+e.getMessage());
            return null;
        }
    }

    public static WxMpTemplateMessage TempTextMsgBuilder(String openid, String msgContent) {
        List<WxMpTemplateData> tmplateDatas = new ArrayList<>();
        WxMpTemplateData first = new WxMpTemplateData("first", msgContent);
        WxMpTemplateData keywords1 = new WxMpTemplateData("keyword1", "");
        WxMpTemplateData keywords2 = new WxMpTemplateData("keyword2", DateTool.formatDate(new Date(), "yyyy-MM-dd"));
        WxMpTemplateData remark = new WxMpTemplateData("remark", "请留意最新动态。");
        tmplateDatas.add(first);
        tmplateDatas.add(keywords1);
        tmplateDatas.add(keywords2);
        tmplateDatas.add(remark);

        return
                WxMpTemplateMessage.builder().templateId("r2yq8UemZB-4KyohWtnXQmazmTHm9Zl4UKsC9-2GnM0")
                        .toUser(openid)
                        .url("https://m.pule.com/kepu/")
                        .miniProgram(new WxMpTemplateMessage.MiniProgram("wx14f319c8964de262",
                                "index?foo=bar"))
                        .data(tmplateDatas)
                        .build();
    }
}
